<template>
  <div class="main-container">
    <div class="goods">
      <van-swipe class="goods-swipe"
                 :autoplay="3000">
        <van-swipe-item v-for="thumb in goods.thumb"
                        :key="thumb">
          <img :src="thumb">
        </van-swipe-item>
      </van-swipe>

      <van-cell-group>
        <van-cell>
          <div class="goods-title">{{ goods.title }}</div>
          <div class="goods-price">{{ formatPrice(goods.price) }}</div>
        </van-cell>
        <van-cell class="goods-express">
          <van-col span="10">运费：{{ goods.express }}</van-col>
          <van-col span="14">剩余：{{ goods.remain }}</van-col>
        </van-cell>
      </van-cell-group>

      <van-cell-group class="goods-cell-group">
        <van-cell value="进入店铺"
                  icon="shop-o"
                  is-link
                  @click="sorry">
          <template slot="title">
            <span class="van-cell-text">有赞的店</span>
            <van-tag class="goods-tag"
                     type="danger">官方</van-tag>
          </template>
        </van-cell>
        <van-cell title="线下门店"
                  icon="location-o"
                  is-link
                  @click="sorry" />
      </van-cell-group>

      <van-cell-group class="goods-cell-group">
        <van-cell title="查看商品详情"
                  is-link
                  @click="sorry" />
      </van-cell-group>

      <van-goods-action>
        <van-goods-action-mini-btn icon="chat-o"
                                   @click="sorry">
          客服
        </van-goods-action-mini-btn>
        <van-goods-action-mini-btn icon="cart-o"
                                   @click="onClickCart">
          购物车
        </van-goods-action-mini-btn>
        <van-goods-action-big-btn @click="sorry">
          加入购物车
        </van-goods-action-big-btn>
        <van-goods-action-big-btn primary
                                  @click="sorry">
          立即购买
        </van-goods-action-big-btn>
      </van-goods-action>
    </div>
  </div>
</template>

<script>
import { getNews, getBanner, getCommunityById } from "@/api/common";
import { mapState, mapGetters } from "vuex";
import commonFun from "@/mixins/common";
export default {
  name: "index",
  mixins: [commonFun],
  data() {
    return {
      newsActive: 0,
      isLoading: false,
      news: [],
      menu: [
        {
          icon: "yuanchengkaimen",
          path: "/code",
          query: {
            target: "tabs"
          },
          title: "开门",
          subtitle: "请出示二维码",
          color: "#58c474"
        },
        {
          icon: "yijian",
          title: "呼叫管家",
          path: "",
          subtitle: "管家式的贴心服务",
          color: "#F99320"
        },
        {
          icon: "iconfont_shezhi",
          title: "报事维修",
          subtitle: "更高效更便捷",
          color: "#6BB8F7",
          query: {
            isWuye: "",
            userType: "",
            communityId: "",
            communityName: ""
          },
          path: "/guarantee"
        },
        {
          icon: "xiaoqu",
          title: "通知公告",
          query: {
            isWuye: "",
            userType: "",
            communityId: "",
            communityName: ""
          },
          subtitle: "小区信息实时掌握",
          color: "#77D1DC",
          path: "/notice"
        }
      ],
      vxCommunityId: "",
      banner: [],
      goods: {
        title: "美国伽力果（约680g/3个）",
        price: 2680,
        express: "免运费",
        remain: 19,
        thumb: [
          "https://img.yzcdn.cn/public_files/2017/10/24/e5a5a02309a41f9f5def56684808d9ae.jpeg",
          "https://img.yzcdn.cn/public_files/2017/10/24/1791ba14088f9c2be8c610d0a6cc0f93.jpeg"
        ]
      }
    };
  },
  watch: {
    // $route() {
    //   this.vxCommunityId = this.$route.query.communityId;
    // },
    // vxCommunityId() {
    //   this.onRefresh();
    //   this.scrollNews();
    // }
  },
  created() {
    // this.onRefresh();
    // this.scrollNews();
  },
  mounted() {
    if (this.roles === this.rolesList.WXG) {
      this.menu[2].path = "/admin/guarantee";
      this.menu[2].title = "维修管理";
    } else if (this.roles === this.rolesList.JL) {
      this.menu[2].path = "/admin/complaint";
      this.menu[2].title = "投诉管理";
    }
  },
  computed: {
    newsFlag() {
      return this.news.length !== 0;
    },
    newsTop() {
      return `${-this.newsActive * 70}px`;
    },
    ...mapGetters(["roles"]),
    ...mapState(["user", "community", "rolesList"])
  },
  methods: {
    formatPrice() {
      return "¥" + (this.goods.price / 100).toFixed(2);
    },
    onClickCart() {
      this.$router.push("cart");
    },
    onRefresh() {
      setTimeout(() => {
        this.newsActive = 0;
        this.isLoading = false;
      }, 500);
      // 获取咨询
      getNews({
        communityId: "68e04e6517d84746ac9e70a6be4bc478",
        page: 1,
        limit: 5,
        type: 0
      }).then(res => {
        const data = res.data;
        this.news = data.records;
      });
      // 获取滚动图
      getBanner("68e04e6517d84746ac9e70a6be4bc478").then(res => {
        const data = res.data;
        const obj = data.obj;
        this.banner = obj || new Array(3);
      });
      //根据小区ID获取小区信息
      getCommunityById("68e04e6517d84746ac9e70a6be4bc478").then(res => {
        var phone = res.data.telphone || res.data.phone;
        if (phone != null && phone.length > 0) {
          this.menu[1].path = "tel:" + phone;
        }
      });
      this.menu[2].query = {
        isWuye: this.$route.query.isWuye,
        userType: this.$route.query.userType,
        communityId: this.$route.query.communityId,
        communityName: this.$route.query.communityName
      };
      this.menu[3].query = {
        isWuye: this.$route.query.isWuye,
        userType: this.$route.query.userType,
        communityId: this.$route.query.communityId,
        communityName: this.$route.query.communityName
      };
    },
    scrollNews() {
      setInterval(() => {
        if (this.newsActive < this.news.length - 1) {
          this.newsActive += 1;
        } else {
          this.newsActive = 0;
        }
      }, 3000);
    }
  }
};
</script>

<style lang="scss" scoped>
.main-container {
  min-height: 100%;
  height: auto;
  overflow: hidden;
  overflow-y: auto;
  padding-bottom: 60px;
  .avue-tip {
    padding: 30px 0;
    padding-right: 50px;
  }
}
//头部
.main-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  color: #fff;
  background-color: rgba($color: #fff, $alpha: 0);
  &::after {
    border: none !important;
  }

  .van-nav-bar__title {
    font-size: 14px !important;
  }
  i {
    color: #fff;
    font-size: 22px;
  }
  .van-nav-bar__right {
    i {
      margin-top: -5px;
    }
  }
}
//banner
.main-banner {
  padding: 0;
  height: 200px;
  .img {
    width: 100%;
    height: 100%;
  }
}
.main-menu {
  padding: 3px 1.5px;
  a {
    text-decoration: none;
  }
  //菜单
  .menu-item {
    margin: 2.5px;
    padding: 28px 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 12px;
    background-color: #fff;
    i {
      margin-right: 12px;
      font-size: 42px;
    }
    .item-title {
      margin-bottom: 15px;
      color: #333;
      font-size: 16px;
    }
    .item-subtitle {
      margin-bottom: 15px;
      font-size: 12px;
      color: #b3b8bc;
    }
  }
}

//新闻
.main-news {
  $color: #ef5b26;
  padding: 15px 27px 8px 27px;
  background-color: #fff;
  .box {
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: flex-start;
    border-bottom: 1px solid #e6e6e6;
  }
  .list {
    width: 100%;
  }
  .menu {
    padding-left: 20px;
    box-sizing: border-box;
    height: 70px;
    overflow: hidden;
  }
  i {
    margin-top: -6px;
    margin-right: 12px;
    color: $color;
    font-size: 36px;
  }
  .name {
    margin-bottom: 14px;
    color: $color;
    font-size: 16px;
  }
  .item {
    margin-bottom: 25px;
    position: relative;
    color: #333;
    text-decoration: none;
    transition: top 0.5s;
    display: block;
    a {
      color: #333;
      text-decoration: none;
    }
    .title {
      width: 70%;
      margin-bottom: 10px;
      font-size: 14px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      height: 20px;
    }
    .datetime {
      color: #ccc;
      font-size: 12px;
    }
  }

  .more {
    margin-bottom: 10px;
    color: #b3b8bc;
    font-size: 14px;
    text-align: right;
    a {
      color: #b3b8bc;
    }
  }
}
//活动
.main-huodong {
  padding: 0 28px 10px 28px;
  box-sizing: border-box;
  background-color: #fff;
  $color: #fe5277;
  .header {
    margin-bottom: 7px;
    display: flex;
    align-items: center;
    height: 40px;
    color: $color;
    overflow: hidden;
    .title {
      font-size: 16px;
    }
    i {
      margin-right: 14px;
      font-size: 30px;
    }
  }
  .list {
    margin-top: 10px;
    width: 100%;
  }
}

.goods {
  padding-bottom: 50px;
  &-swipe {
    img {
      width: 100%;
      display: block;
    }
  }
  &-title {
    font-size: 16px;
  }
  &-price {
    color: #f44;
  }
  &-express {
    color: #999;
    font-size: 12px;
    padding: 5px 15px;
  }
  &-cell-group {
    margin: 15px 0;
    .van-cell__value {
      color: #999;
    }
  }
  &-tag {
    margin-left: 5px;
  }
}
</style>

